package com.xiaochong.tir.apiservice.thread;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.apiservice.service.KLineTradesService;
import com.xiaochong.tir.apiservice.util.SpringContextUtil;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.data.vo.ApiKActionQuantizationVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Description: 定时处理交易对行情
 * @Author: org
 * @CreateDate: 2018/7/9 11:47
 */
public class KLineQuantizationApiThread implements Runnable{

    private Integer exchangeId;
    private Integer type;
    private CountDownLatch countDownLatch;

    public KLineQuantizationApiThread(Integer exchangeId, Integer type, CountDownLatch countDownLatch) {
        this.exchangeId = exchangeId;
        this.type = type;
        this.countDownLatch = countDownLatch;
    }

    private KLineTradesService kLineTradesService =
            (KLineTradesService) SpringContextUtil.getBean("kLineTradesService");

    private RedisServer redisServer =
            (RedisServer) SpringContextUtil.getBean("redisServer");
    @Override
    public void run() {
        LogTrace.beginTrace("thread-KLine组装k线行情列表数据2");
        try {
            String params = String.valueOf(exchangeId!=null?exchangeId:0);
            LogTrace.info("thread-KLine组装k线行情列表数据2","params",type+"-"+params);
            ExchangeTransactionpairMiddle query = new ExchangeTransactionpairMiddle();
            if(type == 1){//精选
                query.setSelected(1);
            }else{
                query.setExchangeId(exchangeId);
            }
            List<ApiKActionQuantizationVo> dataVo = kLineTradesService.getKLineQuantizationExchange(query);
            String redisKey = RedisKeysUtils.API_SERVICE_TRADES_EXCHANGE_SELECT_ALL_DATA+"@";
            //放入不同的redis中
            if(type == 1){//精选
                redisKey +="select@0";
            }else{
                redisKey +="exchange@"+exchangeId;
            }
            // System.out.println(redisKey);
            redisServer.set(redisKey,dataVo!=null&&dataVo.size()>0? JSON.toJSONString(dataVo):"");
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("thread-KLine组装k线行情列表数据2",e);
        }finally {
            countDownLatch.countDown();
        }
    }
}
